package com.dongdong.service.impl;

import com.dongdong.mapper.TbGoodsDescMapper;
import com.dongdong.mapper.TbGoodsMapper;
import com.dongdong.mapper.TbItemMapper;
import com.dongdong.model.TbGoods;
import com.dongdong.model.TbGoodsDesc;
import com.dongdong.model.TbItem;
import com.dongdong.model.TbItemExample;
import com.dongdong.service.GoodsService;
import com.dongdong.utils.JwtUtil;
import com.dongdong.utils.PageBean;
import com.dongdong.utils.PageResult;
import com.dongdong.utils.Result;
import com.dongdong.vo.GoodsDescVO;
import com.dongdong.vo.GoodsVO;
import com.dongdong.vo.ItemVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private TbGoodsMapper goodsMapper;
    @Autowired
    private TbGoodsDescMapper goodsDescMapper;

    @Autowired
    private TbItemMapper itemMapper;

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    @Transactional
    public void addGoods(GoodsVO goodsVO, HttpServletRequest request) {
        String token = request.getHeader("accessToken");
//        商家id
        String sellerId = jwtUtil.parseJwt(token).getId();
        TbGoods goods = new TbGoods();
        BeanUtils.copyProperties(goodsVO, goods);
        goods.setSellerId(sellerId);
        goods.setAuditStatus("0");
        goodsMapper.insertGoods(goods);

        TbGoodsDesc goodsDesc = new TbGoodsDesc();
        BeanUtils.copyProperties(goodsVO.getGoodsDescVO(), goodsDesc);
        goodsDesc.setGoodsId(goods.getId());
        goodsDescMapper.insertSelective(goodsDesc);

    }

    @Override
    public PageResult<GoodsVO> getGoodsPage(PageBean<GoodsVO> goodsVO) {

        PageHelper.startPage(goodsVO.getPageNum(), goodsVO.getPageSize());
        TbGoods goods = new TbGoods();
        BeanUtils.copyProperties(goodsVO, goods);
        List<TbGoods> goodsList = goodsMapper.selectByExample(null);
        PageInfo<TbGoods> info = new PageInfo<>(goodsList);
        List<GoodsVO> voList = goodsList.stream().map((x) -> {
            GoodsVO vo = new GoodsVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageResult<>(info.getPageNum(), info.getPageSize(), info.getTotal(), voList);
    }

    @Override
    public Result updateGoodsStatus(List<Long> ids, String status) {
//       0:未审核 1:审核通过 2:审核中 3:驳回（审核）
        TbGoods goods = null;
        for (Long id : ids) {
            goods = goodsMapper.selectByPrimaryKey(id);
        }
        if (Objects.equals(status, "2")) {
            goods.setAuditStatus(status);
        }
//        运营商  驳回
        if (Objects.equals(status, "3")) {
            goods.setAuditStatus(status);
        }
        if (Objects.equals(status, "1")) {
            goods.setAuditStatus(status);
        }
        goodsMapper.updateByPrimaryKeySelective(goods);
        return Result.OK();
    }

    @Override
    public GoodsVO getGoodsAndDescAndItem(Long goodsId) {
//        根据商品id查询goods
        TbGoods goods = goodsMapper.selectByPrimaryKey(goodsId);
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goods,goodsVO);
//        根据商品id查询goodsdesc
        TbGoodsDesc goodsDesc = goodsDescMapper.selectByPrimaryKey(goodsId);
        GoodsDescVO goodsDescVO = new GoodsDescVO();
        BeanUtils.copyProperties(goodsDesc,goodsDescVO);
//        根据商品id查询ItemVO 集合
        TbItemExample example = new TbItemExample();
        example.createCriteria().andGoodsIdEqualTo(goodsId);
        List<TbItem> tbItems = itemMapper.selectByExample(example);
        List<ItemVO> itemVOList = tbItems.stream().map((x) -> {
            ItemVO itemVO = new ItemVO();
            BeanUtils.copyProperties(x, itemVO);
            return itemVO;
        }).collect(Collectors.toList());

        goodsVO.setGoodsDescVO(goodsDescVO);
        goodsVO.setItemVOS(itemVOList);
        return goodsVO;
    }


}
